package com.a.b.c.d.e.controller;

import com.a.b.c.d.e.entities.*;
import com.a.b.c.d.e.utils.EastMoneyUtil;
import com.a.b.c.d.e.utils.JsonUtil;
import com.google.gson.internal.LinkedTreeMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created on 2021/8/27.
 *
 * @author yanshuchun
 */
@RestController
public class Acontroller {

    @GetMapping("hello")
    public ModelAndView hello(ModelAndView modelAndView) {
        modelAndView.setViewName("f");
        return modelAndView;
    }

    @PostMapping("cci")
    public BollAndCciDataEntity bollAndCciDataEntity() {
        Map<String, String> params = new HashMap<>();
        params.put("fields1", "f1,f2,f3,f4,f5");
        params.put("fields2", "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61");
        params.put("ut", "fa5fd1943c7b386f172d6893dbfba10b");
        params.put("end", "20211011");
        params.put("klt", "101");
        params.put("fqt", "0");
        params.put("lmt", "75");
        params.put("secid", "0.000868");
        params.put("_", System.currentTimeMillis() + "");
        ResponseFormEastMoney<GupiaoEntity> responseFormEastMoney = new ResponseFormEastMoney<>();
        String dataFromEastMoney = EastMoneyUtil.getDataFromEastMoney("http://push2his.eastmoney.com/api/qt/stock/kline/get?", params, null);
        ResponseFormEastMoney<LinkedTreeMap> res = JsonUtil.getGson().fromJson(dataFromEastMoney, responseFormEastMoney.getClass());
        LinkedTreeMap data = res.getData();
        List<String> klines = (List<String>) data.get("klines");
        List<KLinesEntity> kLinesEntities = klines.stream().map((item) -> EastMoneyUtil.stringToKLinesEntity(item)).collect(Collectors.toList());
        BollAndCciDataEntity bollAndCciDataEntity = EastMoneyUtil.boll(kLinesEntities, 20, 0, new BollAndCciDataEntity());
        List<DataEntity> cci = EastMoneyUtil.cci(kLinesEntities, 14, 0, new ArrayList<>());
        bollAndCciDataEntity.setCci(cci);
        return bollAndCciDataEntity;
    }
}
